﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Sublimation
{
    public class Explosion : GameObject
    {
        // the animation
        protected AnimatedSprite.AnimatedTexture textureAnimator;

        //TODO: explosion sound

        public Explosion(WorldProperties worldProperties, GameObject parent, float scaleOfExplosion)
            : base(worldProperties)
        {
           
            // dont store the parent so it gets garbage collected

            //// make sure it loads and draws
            DrawOrder = parent.DrawOrder;
            UpdateOrder = parent.UpdateOrder;
            Visible = true;
            Enabled = true;

            this.boundingRectangle = new Rectangle(0, 0, 1, 1);
            Origin = new Vector2(10, 10);
            //ScaleWithinObject(parent, scaleOfExplosion);
            this.Scale = 1.0f;
            CentreOnObject(parent);
            AlignOriginWithObject(parent);
            this.Rotation = parent.Rotation;
            this.Velocity = Vector2.Zero;

            LoadContent();

 
        }

        private const int NUMBER_OF_FRAMES_TO_PLAY = 5;
        private const int FRAMES_PER_SECOND = 10;
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            // is the animation done?
            if (textureAnimator.FramesUpdated > NUMBER_OF_FRAMES_TO_PLAY)
            {
                Kill();
                return;
            }

            // update the animation
            textureAnimator.UpdateFrame((float)gameTime.ElapsedGameTime.TotalSeconds);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            if (textureAnimator.FramesUpdated > NUMBER_OF_FRAMES_TO_PLAY)
            {
                return;
            }
            
            spriteBatch.Begin();
            textureAnimator.DrawFrame(spriteBatch, Position);
            spriteBatch.End();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
           // Sprite = Game.Content.Load<Texture2D>("Menu");
            textureAnimator = new AnimatedSprite.AnimatedTexture(Origin, Rotation, Scale, 1.0f);
            textureAnimator.Load(Game.Content, "Sprites\\explosion", 5, FRAMES_PER_SECOND);
        }
    }
}